﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Revis.VClinicNet.App.Internal;
using Revis.VClinicNet.App.Internal.Dto;
using Revis.VClinicNet.App.Internal.Dto.Validation;
using Revis.VClinicNet.UI.Internal.Mvc.Extensions;
using Revis.VClinicNet.UI.Internal.Mvc.Infrastructure;
using Revis.VClinicNet.Core.Domain.Credentials;

namespace Revis.VClinicNet.UI.Internal.Mvc.Controllers
{
    public class PatientUserController : Infrastructure.ControllerBase
    {
        private IPatientUserService _patientUserService = null;

        public PatientUserController(IPatientUserService ipus) :
            base("PatientUser")
        {
            _patientUserService = ipus;
        }

        /// <summary>
        /// Index action to show patient status of external account
        /// </summary>
        /// <param name="id">patient id</param>
        /// <returns></returns>
        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Recepcionist | WorkerUserRole.Administrator)]
        public ActionResult Index(long id)
        {
            PatientUserInfoDto patient = _patientUserService.FindUser(id);
            if (patient == null) {
                return ErrorView("Nie znaleziono pacjenta", "Index");
            }
            return View(patient);
        }

        /// <summary>
        /// Action to show confimation view of activating external account
        /// </summary>
        /// <param name="id">patient id</param>
        /// <returns></returns>
        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Recepcionist | WorkerUserRole.Administrator)]
        public ActionResult Activate(long id)
        {
            PatientUserInfoDto patient = _patientUserService.FindUser(id);
            if (patient == null) {
                return ErrorView("Nie znaleziono pacjenta", "Activate");
            }
            return View(patient);
        }

        /// <summary>
        /// Activate external account
        /// </summary>
        /// <param name="patientId">patient id</param>
        /// <returns></returns>
        [HttpPost]
        [HistoryBackLinked(Type = BackLinkType.ModifyForm)]
        [AuthorizeFor(Role = WorkerUserRole.Recepcionist | WorkerUserRole.Administrator)]
        public ActionResult ActivateAccount(long patientId)
        {
            PatientUserActiveDto activatedDto = _patientUserService.Activate(patientId);
            if (activatedDto == null) {
                return ErrorView("Aktywowanie zewnętrznego konta zakończone niepowodzeniem", "ActivateAccount");
            }
            return View("Status", activatedDto);
        }

        /// <summary>
        /// Action to show confirmation view of external account deactivation
        /// </summary>
        /// <param name="id">patient id</param>
        /// <returns></returns>
        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Recepcionist | WorkerUserRole.Administrator)]
        public ActionResult Deactivate(long id)
        {
            PatientUserInfoDto patient = _patientUserService.FindUser(id);
            if (patient == null) {
                return ErrorView("Nie znaleziono pacjenta", "Deactivate");
            }
            return View(patient);
        }

        /// <summary>
        /// Deactivate external account
        /// </summary>
        /// <param name="patientId">patient id</param>
        /// <returns></returns>
        [HttpPost]
        [HistoryBackLinked(Type = BackLinkType.ModifyForm)]
        [AuthorizeFor(Role = WorkerUserRole.Recepcionist | WorkerUserRole.Administrator)]
        public ActionResult DeactivateAccount(long patientId)
        {
            PatientUserInfoDto patient = _patientUserService.Deactivate(patientId);
            if (patient == null) {
                return ErrorView("Deaktywowanie konta zakończone niepowodzeniem", "DeactivateAccount");
            }
            return View(patient);
        }

        /// <summary>
        /// Action to show confirmation view of reseting external account
        /// </summary>
        /// <param name="id">patient id</param>
        /// <returns></returns>
        [HttpGet]
        [HistoryBackLinked]
        [AuthorizeFor(Role = WorkerUserRole.Recepcionist | WorkerUserRole.Administrator)]
        public ActionResult Reset(long id)
        {
            PatientUserInfoDto patient = _patientUserService.FindUser(id);
            if (patient == null) {
                return ErrorView("Nie znaleziono pacjenta", "Reset");
            }
            return View(patient);
        }

        /// <summary>
        /// Reset external account
        /// </summary>
        /// <param name="patientId">patient id</param>
        /// <returns></returns>
        [HttpPost]
        [HistoryBackLinked(Type = BackLinkType.ModifyForm)]
        [AuthorizeFor(Role = WorkerUserRole.Recepcionist | WorkerUserRole.Administrator)]
        public ActionResult ResetAccount(long patientId)
        {
            PatientUserActiveDto activatedDto = _patientUserService.Reset(patientId);
            if (activatedDto == null) {
                return ErrorView("Resetowanie zewnętrznego konta zakończone niepowodzeniem", "ResetAccount");
            }
            return View("Status", activatedDto);
        }
    }
}
